package com.zhang.crm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhang.crm.constant.Constants;
import com.zhang.crm.mapper.TActivitymapper;
import com.zhang.crm.mapper.TUsermapper;
import com.zhang.crm.pojo.TActivity;
import com.zhang.crm.pojo.TUser;
import com.zhang.crm.pojo.req.ActivitySearchPageReq;
import com.zhang.crm.service.ActivityService;
import com.zhang.crm.util.UserUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class ActivityServiceImpl implements ActivityService {
    @Resource
    private TActivitymapper tActivitymapper;

    @Resource
    private TUsermapper tUsermapper;
    /**
     * 获取市场活动信息
     * @return
     */
    @Override
    public PageInfo<TActivity> marketActivity(Integer pageNum) {
        PageHelper.startPage(pageNum, Constants.PAGE_DATA_SIZE);
        List<TActivity> tActivityList = tActivitymapper.selectActivityList();
        PageInfo<TActivity> tActivityPageInfo = new PageInfo<>(tActivityList);
        return tActivityPageInfo;
    }

    /**
     * 获取活动详情
     * @param id
     * @return
     */
    @Override
    public TActivity getActivityDetails(Integer id) {
        TActivity activity = tActivitymapper.selectByPrimaryKeyDetails(id);
        //获取负责人名字
        TUser tUser = tUsermapper.selectByPrimaryKey(activity.getOwnerId());
        activity.setOwnerName(tUser.getName());
        return activity;
    }

    /**
     * 添加市场活动
     */
    @Override
    @Transactional
    public Boolean addActivity(TActivity tActivity) {
        tActivity.setCreateTime(new Date());
        tActivity.setCreateBy(UserUtil.getUserInfo().getId());
        int insert = tActivitymapper.insert(tActivity);
        return insert > 0 ? true : false;
    }

    @Override
    @Transactional
    public Boolean updateActivity(TActivity tActivity) {
        tActivity.setEditBy(UserUtil.getUserInfo().getId());
        tActivity.setEditTime(new Date());
        int i = tActivitymapper.updateByPrimaryKeySelective(tActivity);
        return i > 0 ? true:false;
    }

    @Override
    public Boolean delActivityById(Integer activityId) {
        int i = tActivitymapper.deleteByPrimaryKey(activityId);
        return i > 0 ? true:false;
    }

    @Override
    @Transactional
    public Boolean batchDelActivity(List<Integer> activityIds) {
        int i = tActivitymapper.batchDelActivity(activityIds);
        return i > 0 ? true:false;
    }

    /**
     * 分页条件查询
     * @param activityReq
     * @return
     */
    @Override
    public PageInfo<TActivity> queryActivityPageList(ActivitySearchPageReq activityReq) {
        PageHelper.startPage(activityReq.getCurrent(),Constants.PAGE_DATA_SIZE);
        List<TActivity> tActivityList = tActivitymapper.selectQueryActivityList(activityReq);
        PageInfo result = new PageInfo(tActivityList);
        return result;
    }

    @Override
    public List<TActivity> loadActivity() {
        List<TActivity> tActivityList = tActivitymapper.selectActivityList();
        return tActivityList;
    }
}
